//
//  RTJJiaXiController.m
//  RuiTaiJinProject
//
//  Created by jayson on 2018/12/8.
//  Copyright © 2018年 leewei. All rights reserved.
//

#import "RTJJiaXiController.h"
#import "RTJLendCell.h"
#import "RTJTabBarController.h"
@interface RTJJiaXiController ()

@property (weak, nonatomic) IBOutlet UITableView *tableView;


@end

@implementation RTJJiaXiController

-(void)viewWillAppear:(BOOL)animated{
    
    [self.tableView reloadData];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tableView.tableFooterView=[UIView new];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    self.tableView.estimatedRowHeight=300;
    [self.tableView reloadData];

    if (_isUserCenter) {
        
        [self loadData];
    }    
}

-(void)loadData{
    
    [SVProgressHUD  showWithStatus:nil];
    
    if(self.jiaxiArr.count == 0){
        
        [self.view showEmptyViewWithStr:@"" image:nil callback:nil];
    }
    
    [RTJNetWork asyncRequestWithURL:@"user/get_voucher" params:nil method:GET zxCompletion:^(id data, NSInteger status, BOOL success, NSString *errorMsg) {
        
        [SVProgressHUD dismiss];
        [self.view removeEmptyView];

        if (success) {
            
            if ([data[@"status"]integerValue] == 1) {
                
                self.jiaxiArr = data[@"data"][@"interest_list"];
                
                if(self.jiaxiArr.count == 0){
                    
                    [self.view showEmptyViewWithStr:@"您暂无可用的加息券" image:[UIImage imageNamed:@"kbylogo"] callback:^{
                        
                        [self loadData];
                    }];

                }else{
                    
                    [self.tableView reloadData];

                }
                
                
            }else{
                
                [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",data[@"msg"]]];
                
                [self.view showEmptyViewWithStr:errorMsg image:[UIImage imageNamed:@"kbylogo"] callback:^{
                    
                    [self loadData];
                }];
                
            }
            
        }else{
            
            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",errorMsg]];

            [self.view showEmptyViewWithStr:errorMsg image:[UIImage imageNamed:@"kbylogo"] callback:^{
                
                [self loadData];
            }];
            
        }
        
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return  _jiaxiArr.count;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return UITableViewAutomaticDimension;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * ID = @"RTJLendCell";
    
    RTJLendCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (!cell) {
        
        cell = [[NSBundle mainBundle] loadNibNamed:@"RTJLendCell" owner:self options:nil][2];
    }    
   
    NSInteger isUsed = [_jiaxiArr[indexPath.row][@"can_use_count"] integerValue];//是否使用
    NSInteger out_time = [_jiaxiArr[indexPath.row][@"out_time"] integerValue];//是否过期

    cell.userInteractionEnabled = out_time>0&&isUsed>0 ? YES : NO;
    cell.cardImg.image = out_time>0&&isUsed>0 ? [UIImage imageNamed:@"kqb_jxqljsy"]:[UIImage imageNamed:@"kqb_jxqhuise"];
    cell.cardTitle.text =  [NSString stringWithFormat:@"%@",_jiaxiArr[indexPath.row][@"name"]];
    cell.cardMoneyLab.text = [NSString stringWithFormat:@"%@",_jiaxiArr[indexPath.row][@"rate"]];
    cell.smallLab.text = @"%";
    cell.touzhiLab.text = [NSString stringWithFormat:@"投资金额无限制"];
    cell.useLab.text = @"可与红包叠加使用";
    cell.useCountLab.text = [NSString stringWithFormat:@"该加息券可用次数:%@次",_jiaxiArr[indexPath.row][@""]];
    cell.dateLab.text = [NSString stringWithFormat:@"有效期至：%@",_jiaxiArr[indexPath.row][@"end_time_format"]];
    cell.dateLab.textColor = out_time>0&&isUsed>0 ? [UIColor colorForHex:@"#333333"] :[UIColor colorForHex:@"#CCCCCC"];
    
    if (out_time>0) {
        
        cell.usedLab.text = isUsed>0 ? @"立即使用" :@"已使用";
        
    }else{
        
        cell.usedLab.text =@"已过期";
        
    }
    
    cell.usedLab.textColor =out_time>0&&isUsed>0 ? [UIColor colorForHex:@"#FD3834"] :[UIColor colorForHex:@"#CCCCCC"];
    
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    return  cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (_isUserCenter) {
        
        
        AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        
        RTJTabBarController *tabViewController = (RTJTabBarController *) appDelegate.window.rootViewController;
        
        [tabViewController setSelectedIndex:1];
        
        
    }else{
        
        NSString *str =[NSString stringWithFormat:@"%@",_jiaxiArr[indexPath.row][@"rate"]];
        NSArray *arr = [str componentsSeparatedByString:@"."];
        [self.delegate jiaxiName:[NSString stringWithFormat:@"%@",_jiaxiArr[indexPath.row][@"name"]] rate:[arr[0] integerValue] cardId:[_jiaxiArr[indexPath.row][@"id"] integerValue] min_money:[_jiaxiArr[indexPath.row][@"min_money"] integerValue]];
        
        [self.navigationController popViewControllerAnimated:YES];
        
    }
    
    
    
}

@end
